package com.iss.umservice.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;

import com.iss.umservice.bean.MonthReportBean;
import com.iss.umservice.dao.MonthReportDao;

@Repository
public class MonthReportDaoImpl extends SqlMapClientDaoSupport implements MonthReportDao
{
    
    //查询所有企业往年汇总数据
    @SuppressWarnings("unchecked")
    public List<MonthReportBean> queryLastReport()
    {
        List<MonthReportBean> beanList = getSqlMapClientTemplate().queryForList("report.queryLastReport");
        return beanList;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public MonthReportBean queryCompEndReport(String compId)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("compId", compId);
        
        MonthReportBean bean = (MonthReportBean)getSqlMapClientTemplate().queryForObject("report.queryCompEndReport", args);
        return bean;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public MonthReportBean queryReportExist(String compId, String yearnum, String monthnum)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("compId", compId);
        args.put("yearnum", yearnum);
        args.put("monthnum", monthnum);
        
        MonthReportBean bean = (MonthReportBean)getSqlMapClientTemplate().queryForObject("report.queryReportExist", args);
        return bean;
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public MonthReportBean queryLastReportExist(String compId)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("compId", compId);
        
        MonthReportBean bean = (MonthReportBean)getSqlMapClientTemplate().queryForObject("report.queryLastReportExist", args);
        return bean;
    }
    
    
    //查询企业每年的月报列表
    @SuppressWarnings("unchecked")
    public List<MonthReportBean> queryMonthListComp(String compId, String year)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("compId", compId);
        args.put("yearnum", year);
        
        List<MonthReportBean> beanList = getSqlMapClientTemplate().queryForList("report.queryMonthListComp", args);
        return beanList;
    }
    
    // 按月查询所有企业月报列表
    @SuppressWarnings("unchecked")
    public List<MonthReportBean> queryAllMonthListComp(String yearnum, String monthnum)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("yearnum", yearnum);
        args.put("monthnum", monthnum);
        
        List<MonthReportBean> beanList = getSqlMapClientTemplate().queryForList("report.queryAllMonthListComp", args);
        
        // 求和，放在列表最后一个
        MonthReportBean bean = (MonthReportBean)getSqlMapClientTemplate().queryForObject("report.queryAllMonthListCompSum", args);
        bean.setCompName("总计");
        bean.setCompId("总计");
        
        beanList.add(bean);
        return beanList;
    }
    
    // 增加企业报表
    @SuppressWarnings("unchecked")
    public void addMonthReport(MonthReportBean bean)
    {
        bean.setId("monthreportid");
        
        // 设置季度
        int quart = Integer.parseInt(bean.getMonthnum());
        if(quart >=1 && quart <= 3) 
        {
            bean.setQuart("1");
        }
        else if(quart >=4 && quart <= 6) 
        {
            bean.setQuart("2");
        }
        else if(quart >=7 && quart <= 9) 
        {
            bean.setQuart("3");
        }
        else if(quart >=10 && quart <= 12) 
        {
            bean.setQuart("4");
        }
        getSqlMapClientTemplate().insert("report.addMonthReport", bean);
    }
    
    // 修改企业报表
    @SuppressWarnings("unchecked")
    public void modifyMonthReport(MonthReportBean bean)
    {
        getSqlMapClientTemplate().update("report.modifyMonthReport", bean);
    }
    
 // 增加企业往年报表
    @SuppressWarnings("unchecked")
    public void addLastReport(MonthReportBean bean)
    {
		if (bean.getCompId() != null && !"".equals(bean.getCompId())) 
		{
			deleteLastReport(bean.getCompId());
		}
    	
        bean.setId("lastid");
        
        getSqlMapClientTemplate().insert("report.addLastReport", bean);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public void deleteLastReport(String compId)
    {
//        Map<String,Object> args = new HashMap<String,Object>();
//        args.put("compId", compId);
        
        getSqlMapClientTemplate().delete("report.deleteLastReport", compId);
    }
    
    //园区查询企业月报汇总
    @SuppressWarnings("unchecked")
    public List<MonthReportBean> queryMonthTotal(String benginYear, String endYear, String beginMonth, String endMonth)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("benginYear", benginYear);
        args.put("endYear", endYear);
        args.put("beginMonth", beginMonth);
        args.put("endMonth", endMonth);
        
        int revenue =0;
        int ipInvent = 0;
        int ipNewType = 0;
        int ipLook = 0;
        int icDrawing = 0;
        int softwareProd = 0;
        int softwareBook = 0;
        
        MonthReportBean all = null;
        List<MonthReportBean> beanList = getSqlMapClientTemplate().queryForList("report.queryMonthTotal", args);
        for (int i = 0; i < beanList.size(); i++)
        {
            all = beanList.get(i);
            revenue = revenue + getInt(all.getRevenue());
            ipInvent = ipInvent + getInt(all.getIpInvent());
            ipNewType = ipNewType + getInt(all.getIpNewType());
            ipLook = ipLook + getInt(all.getIpLook());
            icDrawing = icDrawing + getInt(all.getIcDrawing());
            softwareProd = softwareProd + getInt(all.getSoftwareProd());
            softwareBook = softwareBook + getInt(all.getSoftwareBook());
        }
        
        MonthReportBean totalBean = new MonthReportBean();
        totalBean.setRevenue(String.valueOf(revenue));
        totalBean.setIpInvent(String.valueOf(ipInvent));
        totalBean.setIpNewType(String.valueOf(ipNewType));
        totalBean.setIpLook(String.valueOf(ipLook));
        totalBean.setIcDrawing(String.valueOf(icDrawing));
        totalBean.setSoftwareProd(String.valueOf(softwareProd));
        totalBean.setSoftwareBook(String.valueOf(softwareBook));
        if (all != null)
        {
            totalBean.setDoctor(all.getDoctor());
            totalBean.setMaster(all.getMaster());
            totalBean.setBachelor(all.getBachelor());
            totalBean.setCollege(all.getCollege());
            totalBean.setTotalEmployee(all.getTotalEmployee());
        }
        
        beanList.add(totalBean);
        return beanList;
    }
    
  //园区查询企业季度报告汇总
    @SuppressWarnings("unchecked")
    public List<MonthReportBean> queryQuartTotal(String benginYear, String endYear, String beginQuart, String endQuart)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("benginYear", benginYear);
        args.put("endYear", endYear);
        args.put("beginQuart", beginQuart);
        args.put("endQuart", endQuart);
        
        int revenue =0;
        int ipInvent = 0;
        int ipNewType = 0;
        int ipLook = 0;
        int icDrawing = 0;
        int softwareProd = 0;
        int softwareBook = 0;
        
        MonthReportBean all = null;
        List<MonthReportBean> beanList = getSqlMapClientTemplate().queryForList("report.queryQuartTotal", args);
        for (int i = 0; i < beanList.size(); i++)
        {
            all = beanList.get(i);
            MonthReportBean personBean = queryQuartPerson(all.getYearnum(), all.getQuart());
            all.setDoctor(personBean.getDoctor());
            all.setMaster(personBean.getMaster());
            all.setBachelor(personBean.getBachelor());
            all.setCollege(personBean.getCollege());
            all.setTotalEmployee(personBean.getTotalEmployee());
            
            revenue = revenue + getInt(all.getRevenue());
            ipInvent = ipInvent + getInt(all.getIpInvent());
            ipNewType = ipNewType + getInt(all.getIpNewType());
            ipLook = ipLook + getInt(all.getIpLook());
            icDrawing = icDrawing + getInt(all.getIcDrawing());
            softwareProd = softwareProd + getInt(all.getSoftwareProd());
            softwareBook = softwareBook + getInt(all.getSoftwareBook());
        }
        
        MonthReportBean totalBean = new MonthReportBean();
        totalBean.setRevenue(String.valueOf(revenue));
        totalBean.setIpInvent(String.valueOf(ipInvent));
        totalBean.setIpNewType(String.valueOf(ipNewType));
        totalBean.setIpLook(String.valueOf(ipLook));
        totalBean.setIcDrawing(String.valueOf(icDrawing));
        totalBean.setSoftwareProd(String.valueOf(softwareProd));
        totalBean.setSoftwareBook(String.valueOf(softwareBook));
        if (all != null)
        {
            totalBean.setDoctor(all.getDoctor());
            totalBean.setMaster(all.getMaster());
            totalBean.setBachelor(all.getBachelor());
            totalBean.setCollege(all.getCollege());
            totalBean.setTotalEmployee(all.getTotalEmployee());
        }
        
        beanList.add(totalBean);
        return beanList;
    }
    
  //园区查询企业季度报告汇总(查人员数据汇总)
    @SuppressWarnings("unchecked")
    public MonthReportBean queryQuartPerson(String yearnum, String quart)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("yearnum", yearnum);
        args.put("quart", quart);
        
        MonthReportBean bean = (MonthReportBean)getSqlMapClientTemplate().queryForObject("report.queryQuartPerson", args);
        return bean;
    }
    
  //园区查询企业季度列表(不包含人员) ,按企业显示每个季度的
    @SuppressWarnings("unchecked")
    public List<MonthReportBean> queryQuartTotalComp(String year, String quart)
    {
        int revenue =0;
        int ipInvent = 0;
        int ipNewType = 0;
        int ipLook = 0;
        int icDrawing = 0;
        int softwareProd = 0;
        int softwareBook = 0;
        
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("year", year);
        args.put("quart", quart);
        
        MonthReportBean all = null;
        List<MonthReportBean> beanList = getSqlMapClientTemplate().queryForList("report.queryQuartTotalComp", args);
        for (int i = 0; i < beanList.size(); i++)
        {
            all = beanList.get(i);
            MonthReportBean personBean = queryQuartPersonComp(all.getYearnum(), all.getQuart(), all.getCompId());
            all.setDoctor(personBean.getDoctor());
            all.setMaster(personBean.getMaster());
            all.setBachelor(personBean.getBachelor());
            all.setCollege(personBean.getCollege());
            all.setTotalEmployee(personBean.getTotalEmployee());
            
            revenue = revenue + getInt(all.getRevenue());
            ipInvent = ipInvent + getInt(all.getIpInvent());
            ipNewType = ipNewType + getInt(all.getIpNewType());
            ipLook = ipLook + getInt(all.getIpLook());
            icDrawing = icDrawing + getInt(all.getIcDrawing());
            softwareProd = softwareProd + getInt(all.getSoftwareProd());
            softwareBook = softwareBook + getInt(all.getSoftwareBook());
        }
        
        MonthReportBean totalBean = new MonthReportBean();
        totalBean.setRevenue(String.valueOf(revenue));
        totalBean.setIpInvent(String.valueOf(ipInvent));
        totalBean.setIpNewType(String.valueOf(ipNewType));
        totalBean.setIpLook(String.valueOf(ipLook));
        totalBean.setIcDrawing(String.valueOf(icDrawing));
        totalBean.setSoftwareProd(String.valueOf(softwareProd));
        totalBean.setSoftwareBook(String.valueOf(softwareBook));
        if (all != null)
        {
            totalBean.setDoctor(all.getDoctor());
            totalBean.setMaster(all.getMaster());
            totalBean.setBachelor(all.getBachelor());
            totalBean.setCollege(all.getCollege());
            totalBean.setTotalEmployee(all.getTotalEmployee());
        }
        beanList.add(totalBean);
        return beanList;
    }
    
  //园区查询企业季度列表(按季度汇总人员数量),按企业显示每个季度的
    @SuppressWarnings("unchecked")
    public MonthReportBean queryQuartPersonComp(String yearnum, String quart, String compId)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("yearnum", yearnum);
        args.put("quart", quart);
        args.put("compId", compId);
        
        MonthReportBean bean = (MonthReportBean)getSqlMapClientTemplate().queryForObject("report.queryQuartPersonComp", args);
        return bean;
    }
    
  //园区查询企业年度报告汇总
    @SuppressWarnings("unchecked")
    public List<MonthReportBean> queryYearTotal(String benginYear, String endYear)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("benginYear", benginYear);
        args.put("endYear", endYear);
        
        int revenue =0;
        int ipInvent = 0;
        int ipNewType = 0;
        int ipLook = 0;
        int icDrawing = 0;
        int softwareProd = 0;
        int softwareBook = 0;
        
        MonthReportBean all = null;
        List<MonthReportBean> beanList = getSqlMapClientTemplate().queryForList("report.queryYearTotal", args);
        for (int i = 0; i < beanList.size(); i++)
        {
            all = beanList.get(i);
            MonthReportBean personBean = queryYearPerson(all.getYearnum());
            all.setDoctor(personBean.getDoctor());
            all.setMaster(personBean.getMaster());
            all.setBachelor(personBean.getBachelor());
            all.setCollege(personBean.getCollege());
            all.setTotalEmployee(personBean.getTotalEmployee());
            
            revenue = revenue + getInt(all.getRevenue());
            ipInvent = ipInvent + getInt(all.getIpInvent());
            ipNewType = ipNewType + getInt(all.getIpNewType());
            ipLook = ipLook + getInt(all.getIpLook());
            icDrawing = icDrawing + getInt(all.getIcDrawing());
            softwareProd = softwareProd + getInt(all.getSoftwareProd());
            softwareBook = softwareBook + getInt(all.getSoftwareBook());
        }
        
        MonthReportBean totalBean = new MonthReportBean();
        totalBean.setRevenue(String.valueOf(revenue));
        totalBean.setIpInvent(String.valueOf(ipInvent));
        totalBean.setIpNewType(String.valueOf(ipNewType));
        totalBean.setIpLook(String.valueOf(ipLook));
        totalBean.setIcDrawing(String.valueOf(icDrawing));
        totalBean.setSoftwareProd(String.valueOf(softwareProd));
        totalBean.setSoftwareBook(String.valueOf(softwareBook));
        if (all != null)
        {
            totalBean.setDoctor(all.getDoctor());
            totalBean.setMaster(all.getMaster());
            totalBean.setBachelor(all.getBachelor());
            totalBean.setCollege(all.getCollege());
            totalBean.setTotalEmployee(all.getTotalEmployee());
        }
        
        beanList.add(totalBean);
        return beanList;
    }
    
  //园区查询企业年度报告汇总(查人员数据汇总)
    @SuppressWarnings("unchecked")
    public MonthReportBean queryYearPerson(String yearnum)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("yearnum", yearnum);
        
        MonthReportBean bean = (MonthReportBean)getSqlMapClientTemplate().queryForObject("report.queryYearPerson", args);
        return bean;
    }
    
  //园区查询企业年度列表(不包含人员) ,按企业显示每个年度的
    @SuppressWarnings("unchecked")
    public List<MonthReportBean> queryYearTotalComp(String year)
    {
        int revenue =0;
        int ipInvent = 0;
        int ipNewType = 0;
        int ipLook = 0;
        int icDrawing = 0;
        int softwareProd = 0;
        int softwareBook = 0;
        
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("year", year);
        
        MonthReportBean all = null; 
        List<MonthReportBean> beanList = getSqlMapClientTemplate().queryForList("report.queryYearTotalComp", args);
        for (int i = 0; i < beanList.size(); i++)
        {
            all = beanList.get(i);
            MonthReportBean personBean = queryYearPersonComp(all.getYearnum(), all.getCompId());
            all.setDoctor(personBean.getDoctor());
            all.setMaster(personBean.getMaster());
            all.setBachelor(personBean.getBachelor());
            all.setCollege(personBean.getCollege());
            all.setTotalEmployee(personBean.getTotalEmployee());
            
            revenue = revenue + getInt(all.getRevenue());
            ipInvent = ipInvent + getInt(all.getIpInvent());
            ipNewType = ipNewType + getInt(all.getIpNewType());
            ipLook = ipLook + getInt(all.getIpLook());
            icDrawing = icDrawing + getInt(all.getIcDrawing());
            softwareProd = softwareProd + getInt(all.getSoftwareProd());
            softwareBook = softwareBook + getInt(all.getSoftwareBook());
        }
        
        MonthReportBean totalBean = new MonthReportBean();
        totalBean.setRevenue(String.valueOf(revenue));
        totalBean.setIpInvent(String.valueOf(ipInvent));
        totalBean.setIpNewType(String.valueOf(ipNewType));
        totalBean.setIpLook(String.valueOf(ipLook));
        totalBean.setIcDrawing(String.valueOf(icDrawing));
        totalBean.setSoftwareProd(String.valueOf(softwareProd));
        totalBean.setSoftwareBook(String.valueOf(softwareBook));
        if (all != null)
        {
            totalBean.setDoctor(all.getDoctor());
            totalBean.setMaster(all.getMaster());
            totalBean.setBachelor(all.getBachelor());
            totalBean.setCollege(all.getCollege());
            totalBean.setTotalEmployee(all.getTotalEmployee());
        }
        beanList.add(totalBean);
        
        return beanList;
    }
    
    private int getInt(String s)
    {
        if (s == null || "".equals(s))
        {
            return 0;
        }
        else
        {
            try
            {
                return Integer.parseInt(s);
            }
            catch (Exception e)
            {
                return 0;
            }
            
        }
    }
    
    //园区查询企业年度列表(按季度汇总人员数量),按企业显示每个年度的
    @SuppressWarnings("unchecked")
    public MonthReportBean queryYearPersonComp(String yearnum, String compId)
    {
        Map<String,Object> args = new HashMap<String,Object>();
        args.put("yearnum", yearnum);
        args.put("compId", compId);
        
        MonthReportBean bean = (MonthReportBean)getSqlMapClientTemplate().queryForObject("report.queryYearPersonComp", args);
        return bean;
    }
}
